package org.example;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

public class ImageDragHandler {
    private ImageView imageView;
    private DoubleProperty mouseX = new SimpleDoubleProperty();
    private DoubleProperty mouseY = new SimpleDoubleProperty();
    // 灵敏度设置，可根据需求调整，数值越大，拖动时视角移动越明显
    private double sensitivity = 0.5;

    public ImageDragHandler(ImageView imageView) {
        this.imageView = imageView;
        setupMouseListeners();
    }

    private void setupMouseListeners() {
        imageView.setOnMousePressed(this::handleMousePressed);
        imageView.setOnMouseDragged(this::handleMouseDragged);
    }

    private void handleMousePressed(MouseEvent event) {
        System.out.println("Mouse Pressed on ImageView at: " + event.getX() + ", " + event.getY());
        mouseX.set(event.getX());
        mouseY.set(event.getY());
    }

    private void handleMouseDragged(MouseEvent event) {
        System.out.println("Mouse Dragged on ImageView, offset: " + (event.getX() - mouseX.get()) + ", " + (event.getY() - mouseY.get()));
        double offsetX = (event.getX() - mouseX.get()) * sensitivity;
        double offsetY = (event.getY() - mouseY.get()) * sensitivity;
        System.out.println("Calculated offsetX: " + offsetX + ", offsetY: " + offsetY);

        // 获取当前图片在ImageView中的显示位置
        double currentTranslateX = imageView.getTranslateX();
        double currentTranslateY = imageView.getTranslateY();

        // 更新显示位置
        imageView.setTranslateX(currentTranslateX + offsetX);
        imageView.setTranslateY(currentTranslateY + offsetY);

        mouseX.set(event.getX());
        mouseY.set(event.getY());
    }

    // 可提供方法用于设置灵敏度，方便外部根据需求调整
    public void setSensitivity(double sensitivity) {
        this.sensitivity = sensitivity;
    }
}